<?php

class Backend_AuthController extends Zend_Controller_Action
{
    protected $_redirector;

    public function init()
    {
        /* Initialize action controller here */
        $this->_helper->layout->setLayout('backend/layout');
        $this->view->title = "";

        $this->_redirector = $this->_helper->getHelper('Redirector');

        // check session
        @session_start();
    }

    public function indexAction()
    {
        // action body
    }

    public function loginAction()
    {
        $this->view->title = "Login";

        $form = new Backend_Form_AdminLogin();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();
            
            // get admin
            $admin_db = new Backend_Model_DbTable_Admin();
            $admin = $admin_db->getAdminByEmail($postData['email']);

            // Check existence
            if(!$admin){
                $error = array('message'=>'Wrong email/password','prev'=>'/backend/auth/login');
                $_SESSION['error'] = $error;
                $this->_redirector->gotoUrl('/backend/error');
            }

            // Check password
            if(strcmp($admin['password'], sha1($postData['password'])) != 0){
                $error = array('message'=>'Wrong email/password','prev'=>'/backend/auth/login');
                $_SESSION['error'] = $error;
                $this->_redirector->gotoUrl('/backend/error');
            }

            $_SESSION['admin'] = $admin;
            $this->_redirector->gotoUrl('/backend/');
        }
    }

    public function logoutAction()
    {
        // action body
        if(isset($_SESSION['admin'])){
            unset ($_SESSION['admin']);
        }

        $this->_redirector->gotoUrl('/backend/');
    }


}





